﻿using Newtonsoft.Json;
using RestSharp;
using System;
using System.Collections.Generic;

namespace ACSynologyHelper
{
    public class SynologyHelper
    {
        /// <summary>
        /// 初始化Synoloigy类
        /// </summary>
        /// <param name="url">地址与端口</param>
        public SynologyHelper(string url)
        {
            if (url != "")
            {
                SynologyConf.SynologyURL = url;
            }
            SynologyConf.RClient = new RestClient(SynologyConf.SynologyURL);
            SynologyConf.RRequest = new RestRequest();
        }

        /// <summary>
        /// 查询所有API
        /// </summary>
        /// <param name="queryItem">API名称 all=所有</param>
        /// <returns></returns>
        public SynologyResult Query(string queryItem = "all")
        {
            SynologyConf.RRequest.Resource = "webapi/query.cgi";
            SynologyConf.RRequest.Parameters.Clear();
            SynologyConf.RRequest.AddParameter("api", "SYNO.API.Info", ParameterType.GetOrPost);
            SynologyConf.RRequest.AddParameter("version", "1", ParameterType.GetOrPost);
            SynologyConf.RRequest.AddParameter("method", "query", ParameterType.GetOrPost);
            SynologyConf.RRequest.AddParameter("query", queryItem, ParameterType.GetOrPost);
            var r = SynologyConf.RClient.Execute(SynologyConf.RRequest, Method.GET);
            return JsonConvert.DeserializeObject<SynologyResult>(r.Content);
        }

        /// <summary>
        /// 登陆Synology
        /// </summary>
        /// <param name="user">用户名</param>
        /// <param name="pass">密码</param>
        /// <param name="sessionType">会话类型</param>
        /// <returns></returns>
        public SynologyResult Login(string user,string pass,SessionType sessionType)
        {
            SynologyConf.RRequest.Resource = "webapi/auth.cgi";
            SynologyConf.RRequest.Parameters.Clear();
            SynologyConf.RRequest.Parameters.AddRange(SynologyConf.ParameterCookies);
            SynologyConf.RRequest.AddParameter("api", "SYNO.API.Auth", ParameterType.GetOrPost);
            SynologyConf.RRequest.AddParameter("version", "3", ParameterType.GetOrPost);
            SynologyConf.RRequest.AddParameter("method", "login", ParameterType.GetOrPost);
            SynologyConf.RRequest.AddParameter("account", user, ParameterType.GetOrPost);
            SynologyConf.RRequest.AddParameter("passwd", pass, ParameterType.GetOrPost);
            SynologyConf.RRequest.AddParameter("session", sessionType.ToString(), ParameterType.GetOrPost);
            SynologyConf.RRequest.AddParameter("format", "sid", ParameterType.GetOrPost);
            var r = SynologyConf.RClient.Execute(SynologyConf.RRequest, Method.POST);
            SynologyConf.Cookies = r.Cookies;
            return JsonConvert.DeserializeObject<SynologyResult>(r.Content);
        }

        public SynologyResult Logout(SessionType sessionType)
        {
            SynologyConf.RRequest.Resource = "webapi/auth.cgi";
            SynologyConf.RRequest.Parameters.Clear();
            SynologyConf.RRequest.Parameters.AddRange(SynologyConf.ParameterCookies);
            SynologyConf.RRequest.AddParameter("api", "SYNO.API.Auth", ParameterType.GetOrPost);
            SynologyConf.RRequest.AddParameter("version", "1", ParameterType.GetOrPost);
            SynologyConf.RRequest.AddParameter("method", "logout", ParameterType.GetOrPost);
            SynologyConf.RRequest.AddParameter("session", sessionType.ToString(), ParameterType.GetOrPost);
            var r = SynologyConf.RClient.Execute(SynologyConf.RRequest, Method.GET);
            return JsonConvert.DeserializeObject<SynologyResult>(r.Content);
        }

        
    }
}
